<template>
	<view class="login">
		<view class="log">
			<image src="../../static/logo.png" mode=""></image>
		</view>
		<!-- <button open-type="getPhoneNumber" class="getphone_btn" @getphonenumber="getNumber"></button> -->


		<view class="wxlo" style="position: relative;z-index: 999;">
			<button class="wxBtn" open-type="getUserInfo" @getuserinfo="getUserInfo">微信一键登录</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onMounted,
		ref,
		defineProps,
		watch,
		reactive
	} from 'vue'
	import {RSAencrypt,RSAdecrypt} from "../../utils/rsa.js"
	// 微信登录授权
	const userInfo = ref({})
	let hasUserInfo = ref(false)


	const getInfoAll = () => {
		let deOpenId = RSAencrypt(uni.getStorageSync('openid'))
		uni.request({
			url: 'https://www.chargesystem.cn/charge4gv2/wechat/getUserInfo', // 你的登录API地址
			method: 'GET',
			data: {
				openid:deOpenId// uni.getStorageSync('openid'),
			},
			success: (res) => {
				console.log(res,uni.getStorageSync('openid'),'登录页面')
				// userInfo.value = res.data.result
				// console.log(res.data.result.vin, 'res')
				// 跳转页面 回首页
				uni.removeStorageSync('vin')
				if(res.data.result.vin){
				uni.setStorageSync('vin', res.data.result.vin);	
				}
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			fail: () => {
				uni.showToast({
					title: '请求失败',
					icon: 'none'
				});
			}
		});
	}

	const getUserInfo = (e) => {
		console.log(e)
		var detail = e.detail
		if (e.detail.userInfo) {
			userInfo.value = e.detail.userInfo;
			console.log(userInfo.value, 'userInfo.value')
			hasUserInfo.value = true;
			uni.showToast({
				title: '正在登录中...',
				icon: 'none'
			});
			login();
		} else {
			uni.showModal({
				title: '警告',
				content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!',
				showCancel: false,
				confirmText: '返回授权',
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击了“返回授权”');
					}
				}
			});
		}
	}
	// 登录并获取用户信息
	const login = () => {
		
		uni.login({
			provider: 'weixin',
			success: (loginRes) => {
				// 登录成功，获取用户code
				const {
					code
				} = loginRes;

				console.log(code, 'codedddddddddddddddddd')
				let deCode = RSAencrypt(code)
				
				// 发送code到后台换取openId, sessionKey, unionId
				// uni.setStorageSync('userInfo', this.userInfo);
				// 
				// uni.setStorageSync('sessionKey', sessionKey);
				// uni.setStorageSync('unionId', unionId);



				uni.request({
					url: 'https://www.chargesystem.cn/charge4gv2/wechat/login?code=' +
					deCode, // 你的登录API地址
					method: 'GET',

					success: (res) => {
						console.log(res, 'res')
						if (res.data && res.data.code === 200) {
							const {
								openid,
								sessionKey
							} = res.data.result;
							uni.setStorageSync('openid', openid);
							uni.setStorageSync('sessionKey', sessionKey);
							uni.setStorageSync('isLogin', true);

							// 登录成功后获取相关信息
							getInfoAll()


							// 将用户信息和session存储到本地


						} else {
							uni.showToast({
								title: '登录失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '请求失败',
							icon: 'none'
						});
					}
				});
			},
			fail: (err) => {
				console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err);
				uni.showToast({
					title: '登录失败',
					icon: 'none'
				});
			}
		});
	}
</script>

<style lang="scss" scoped>
	.login {
		background-color: #fff;
		min-height: 1400rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.log {
		padding: 100rpx;

		image {
			width: 100rpx;
			height: 100rpx;
			margin-bottom: 40rpx;
		}
	}

	.wxlo {
		width: 400rpx;

		.wxBtn {
			background-color: #2b9939;
			color: #fff;
		}
	}
</style>